package cn.stylefeng.guns.modular.manager.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.model.params.ArchiveInfoReportType;
import cn.stylefeng.guns.modular.basic.service.ArchiveDictService;
import cn.stylefeng.guns.modular.manager.model.params.ArchiveInfoReportParam;
import cn.stylefeng.guns.modular.manager.model.result.ArchiveInfoReport;
import cn.stylefeng.guns.modular.manager.service.ArchiveInfoService;
import cn.stylefeng.guns.sys.modular.system.entity.Dept;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.guns.tree.LayuiTree;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/archiveInfoReport")
public class ArchiveInfoReportController extends BaseController {
    private String PREFIX = "/manager/archiveInfoReport";
    @Autowired
    private ArchiveInfoService archiveInfoService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private ArchiveDictService archiveDictService;

    /**
     * 跳转到主页面
     *
     * @author blesslp
     * @Date 2019-11-20
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/archiveInfoReport.html";
    }


    @RequestMapping("/report")
    @ResponseBody
    public LayuiPageInfo report(ArchiveInfoReportParam param) {
        final List<ArchiveInfoReportType> types = buildTypeTitle(param.getReportType());
        final List<ArchiveInfoReport> report = this.archiveInfoService.report(param);
        final List<ArchiveInfoReport> data = new ArrayList<>();

        for (ArchiveInfoReportType type : types) {

            final List<ArchiveInfoReport> collect = report.parallelStream().filter(it -> {
                if (ToolUtil.isOneEmpty(type.getDeptId(), type.getTypeValue())) {
                    if (!ToolUtil.isEmpty(type.getDeptId())) {
                        return Objects.equals(type.getDeptId(), it.getSsbmid());
                    } else {
                        return Objects.equals(type.getTypeValue(), it.getDalx());
                    }
                } else {
                    return Objects.equals(type.getTypeValue(), it.getDalx()) && Objects.equals(type.getDeptId(), it.getSsbmid());
                }
            }).collect(Collectors.toList());

            if (ToolUtil.isEmpty(collect)) {
                //没有数据，全是0
                ArchiveInfoReport empty = new ArchiveInfoReport();
                empty.setTitle(type.getTitle());
                empty.setXh(type.getXh());
                data.add(empty);
            }else {
                final ArchiveInfoReport archiveInfoReport = ArchiveInfoReport.addOther(collect);
                archiveInfoReport.setTitle(type.getTitle());
                archiveInfoReport.setXh(type.getXh());
                data.add(archiveInfoReport);
            }
        }

        Page page = new Page(1, data.size(), data.size());
        page.setRecords(data);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * @param type [0]先部门后类型  [1]先类型后部门
     * @return
     */
    private List<ArchiveInfoReportType> buildTypeTitle(String type) {
        final List<Dept> deptList = deptService.list();
        final List<LayuiTree> layuiTrees = this.archiveDictService.listTypeDict();

        //先组装一个文本样式的  档案类型树
        final List<Map<String, String>> dalxTree = Lists.newArrayList();

        //套入
        List<ArchiveInfoReportType> types = new ArrayList<>();
        type = ToolUtil.isEmpty(type) ? "0" : type;
        if ("0".equals(type)) {
            typeNodes(true, "", 1, dalxTree, layuiTrees);
            //先部门后类型
            for (int i = 0; i < deptList.size(); i++) {
                final Dept dept = deptList.get(i);
                StringBuilder xh = new StringBuilder("" + (i + 1));
                types.add(new ArchiveInfoReportType(xh.toString(), dept.getFullName(), dept.getDeptId().toString(), null));

                for (Map<String, String> dalx : dalxTree) {
                    types.add(new ArchiveInfoReportType(xh.toString() + dalx.get("xh"), dalx.get("title"), dept.getDeptId().toString(), dalx.get("value")));
                }
            }
        } else {
            typeNodes(false, "", 1, dalxTree, layuiTrees);

            for (Map<String, String> dalx : dalxTree) {
                types.add(new ArchiveInfoReportType(dalx.get("xh"), dalx.get("title"), null, dalx.get("value")));
                if ("1".equals(dalx.get("isLeaf"))) {
                    //叶子节点
                    for (int i = 0; i < deptList.size(); i++) {
                        final Dept dept = deptList.get(i);
                        StringBuilder xh = new StringBuilder("" + (i + 1));
                        types.add(new ArchiveInfoReportType(dalx.get("xh") + "." + xh.toString(), departStr(Integer.parseInt(dalx.get("level")) + 1) + dept.getFullName(), dept.getDeptId().toString(), dalx.get("value")));
                    }
                }
            }
        }
        return types;
    }

    private void typeNodes(boolean isTopLevelNeedPoint, String xh, int level, List<Map<String, String>> ret, List<LayuiTree> nodes) {
        for (int i = 0; i < nodes.size(); i++) {
            if (isTopLevelNeedPoint && level == 1) {
                xh = ".";
            } else {
                xh = level > 1 && !xh.endsWith(".") ? xh + "." : xh;
            }

            final LayuiTree layuiTree = nodes.get(i);
            Map<String, String> node = Maps.newHashMap();
            node.put("xh", xh + (i + 1));
            node.put("title", departStr(level) + layuiTree.getName());
            node.put("value", layuiTree.getValue());
            node.put("isLeaf", "0");
            node.put("level", level + "");
            ret.add(node);
            if (!ToolUtil.isEmpty(layuiTree.getChildren())) {
                typeNodes(isTopLevelNeedPoint, node.get("xh"), level + 1, ret, layuiTree.getChildren());
            } else {
                node.put("isLeaf", "1");
            }
        }
    }

    private static String departStr(int depth) {
        StringBuilder sTemp = new StringBuilder();
        for (int i = 0; i < depth; i++) {
            sTemp.append("--");
        }
        return sTemp.toString();
    }
}
